package com.demo.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.CharEncoding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class YSMessageHandler {

    /**
     * 邮件发送正常队列
     * @param message
     * @param channel
     * @param headers
     * @throws IOException
     */
//    @RabbitListener(queues ="delay.demo.normal")
//    @RabbitHandler
//    public void handleEmailMessage(Message message, Channel channel, @Headers Map<String,Object> headers) throws IOException {
//
//        try {
//
//            String msg=new String(message.getBody(), CharEncoding.UTF_8);
//            JSONObject jsonObject = JSON.parseObject(msg);
//            jsonObject.put("messageId",headers.get("spring_returned_message_correlation"));
//            log.info("---正常接受到消息---{}",jsonObject);
//            //主动异常
//           // int m=1/0;
//            //手动签收
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//        }
//        catch (Exception e) {
//            log.info("正常队列handleEmailMessage捕获到异常,拒绝重新入队---消息ID---{}", headers.get("spring_returned_message_correlation"));
//            //异常，ture 重新入队,或者false,进入死信队列
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
//
//        }
//    }



    /**
     * 延时队列消费
     * @param message
     * @param channel
     * @param headers
     * @throws IOException
     */
    @RabbitListener(queues ="delayQueue")
    //@RabbitHandler
    public void handleOrderDelayMessage(Message message, Channel channel, @Headers Map<String,Object> headers) throws IOException {

        try {

            String msg=new String(message.getBody(), CharEncoding.UTF_8);
            JSONObject jsonObject = JSON.parseObject(msg);
            MessageProperties messageProperties = message.getMessageProperties();
            String msgId = (String) headers.get("spring_returned_message_correlation");

            String id = messageProperties.getCorrelationId();
            log.info("---延时队列接受到订单消息---orderId---{}",message.getMessageProperties().getMessageId());
            log.info("---延时队列接受到订单消息---CorrelationId---{}",msgId);
            log.info("---订单信息---order---{}",jsonObject);
            //业务逻辑，根据订单id获取订单信息，如果还未支付，设置关闭状态，如果已支付，不做任何处理
            //手动签收
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        catch (Exception e) {
            log.info("延时队列--handleOrderDelayMessage捕获到异常,重新入队---orderId---{}", headers.get("spring_returned_message_correlation"));
            //异常，ture 重新入队,或者false,进入死信队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);

        }
    }


}
